#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2011
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author: Jesus Carrero <j.o.carrero@gmail.com>
# Mountain View, CA

import logging


class calibrationParser:

    __slots__ = ['vK', 'vS0', 'vT', 'vSTYLE', 'vCONTRACT',
                 'cK', 'cS0', 'cT', 'cSTYLE', 'cCONTRACT',
                 'REG', 'M_N_ITER', 'RANDOM_TRAILS', 'INITIAL_GUESS',
                 'm_plot_results', 'LOG_LEVEL', 'LOG_FILE', 'logParser']

    def __init__(self, file_name):
        self.logParser = logging.getLogger('calibrationParser')
        self._load_parameter_file(file_name)
        self._chkParams()

        (self.m_conf, self.m_plot_results) = (None, None)
        (self.vK, self.vS0, self.vT) = (None, None, None)
        (self.cK, self.cS0, self.cT) = (None, None, None)
        (self.m_vstyle, self.m_vcontract) = (None, None)
        (self.m_reg, self.M_N_ITER) = (None, None)
        (self.m_log_level, self.m_log_file) = (None, None)
        (self.random_trails, self.initial_guess) = (None, None)
        (self.cSTYLE, self.m_ccontract) = (None, None)

    def _load_parameter_file(self, file_name):
        try:
            self.m_conf = bc.parse_conf_file(file_name)
        except:
            self.logParser.error('Could not open file: ' + file_name)

        try:

            # [CALIBRATION]

            (self.cK, self.cS0, self.cT) = \
                (self.m_conf.getfloat('CALIBRATION', 'STRIKES'),
                 self.m_conf.getfloat('CALIBRATION', 'PRICES'),
                 self.m_conf.getfloat('CALIBRATION', 'MATURITY'))

            (self.cSTYLE, self.m_ccontract) = \
                (self.m_conf.get('CALIBRATION', 'STYLE'),
                 self.m_conf.get('CALIBRATION', 'CONTRACT'))
        except:
            self.logParser.error('CALIBRATION INFO INCOMPLETE')

        try:

            # [VERIFICATION]

            (self.vK, self.vS0, self.vT) = \
                (self.m_conf.getfloat('VERIFICATION', 'STRIKES'),
                 self.m_conf.getfloat('VERIFICATION', 'PRICES'),
                 self.m_conf.getfloat('VERIFICATION', 'MATURITY'))

            (self.m_vstyle, self.m_vcontract) = \
                (self.m_conf.get('VERIFICATION', 'STYLE'),
                 self.m_conf.get('VERIFICATION', 'CONTRACT'))
        except:
            self.logParser.error('VERIFICATION INFO INCOMPLETE')


        try:

            # [OPTIMIZATON]

            (self.m_reg, self.M_N_ITER) = \
                (self.m_conf.getfloat('OPTIMIZATON', 'REGULARIZATION'),
                 self.m_conf.getfloat('OPTIMIZATON', 'MAX_NUM_ITER'))
            (self.random_trails, self.initial_guess) = \
                (self.m_conf.getboolean('OPTIMIZATON', 'RANDOM_TRAILS'
                 ), self.m_conf.getfloat('OPTIMIZATON', 'INITIAL_GUESS'
                 ))
        except Exception: # as x:
            self.logParser.error('OPTIMIZATION INCOMPLETE')

        try:

            # [OUTPUT]

            self.m_plot_results = self.m_conf.getboolean('OUTPUT',
                    'm_plot_results')
        except Exception:# as x:
            self.logParser.error('OUTPUT INCOMPLETE')

        try:

            # [DEBUG]

            (self.m_log_level, self.m_log_file) = \
                (self.m_conf.get('DEBUG', 'LOG_LEVEL'),
                 self.m_conf.get('DEBUG', 'LOG_FILE'))
        except Exception:#  as x:
            self.logParser.error('DEBUG INCOMPLETE')


